home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 52 / Amiga Format AFCD52 (Issue 136, May 2000).iso / -in_the_mag- / workbench / adf / trackwiz / trackwizard.c < prev    next >
C/C++ Source or Header  |  2000-03-05  |  5KB  |  205 lines

  1.  
  2. /*
  3.  * TrackWizard.c by Jarkko Vatjus-Anttila & Ville Helin (c) 1997.
  4.  *
  5.  * USAGE:
  6.  *
  7.  * (read|write) UNIT (num) FILE (name) [startcyl (num) endcyl (num)]
  8.  *
  9.  * EXAMLE:
  10.  *
  11.  * TrackWizard read UNIT 0 FILE t:output startcyl 50 endcyl 79
  12.  *
  13.  * This is public domain!
  14.  *
  15.  * KNOWN BUGZ:
  16.  *
  17.  * 1) How to detect if the DoIO() returns a failure? With IoERR() perhaps?
  18.  * 2) How to stop trackdisk motor after action??
  19.  *
  20.  */
  21.  
  22. #include "exec/types.h"
  23. #include "exec/memory.h"
  24. #include "exec/io.h"
  25. #include "dos/dos.h"
  26.  
  27. #define cmd_write    1
  28. #define    cmd_read    2
  29. #define    cylsize        11264
  30.  
  31. struct IOStdReq *io = NULL;
  32. struct MsgPort *msg = NULL;
  33. ULONG trackflag;
  34.  
  35. char info[] = "TrackWizard v1.0 by Jarkko Vatjus-Anttila & Ville Helin";
  36. char usagetxt[] = "USAGE: READ|WRITE FILE (name) UNIT (num) [STARTCYL (cyl) ENDCYL (cyl)]";
  37.  
  38. void cleanup(char *error);
  39. BOOL stringcmp(char *t1, char *t2);
  40.  
  41. /*****************************************************************************/
  42.  
  43. main(int argc, char *args[])
  44.     {
  45.     APTR *file;
  46.     char *fname = NULL;
  47.     ULONG action = 0, unit = 500, startcyl = 0, endcyl = 79, i;
  48.     UBYTE *buffer;
  49.  
  50.     printf("%s\n", info);
  51.  
  52.     msg = (struct MsgPort *)CreateMsgPort();
  53.     if (msg == NULL) cleanup("Not enough memory for Message port!");
  54.  
  55.     io = (struct IOStdReq *)CreateIORequest(msg, sizeof(struct IOStdReq));
  56.     if (io == NULL) cleanup("Unable to create I/O request!");
  57.  
  58.  
  59.  
  60. /* Seuraavassa kerätään hieman parametrejä. Olisi ollut hienoa tehdä
  61.  * parseri erikseen, mutta mitä hemmettiä. Täähän on vain tällanen
  62.  * pikku ohjelma.
  63.  *
  64.  * Tarkistetaan mikä komento: read vai write??
  65.  */
  66.  
  67.     if (stringcmp(args[1], "write") == TRUE) action = cmd_write;
  68.     if (stringcmp(args[1], "read") == TRUE) action = cmd_read;
  69.     if (action == 0) cleanup("*** No action (read|write) specified");
  70.  
  71. /*
  72.  *    Sitten tarkistetaan onko tiedoston nimeä annettu!
  73.  */
  74.  
  75.     for (i=1; i<argc; i++) if (stringcmp(args[i], "file") == TRUE) fname = args[i+1];
  76.     if (fname == NULL) cleanup("*** No file name specified!");
  77.  
  78. /*
  79.  * Ja sitten unitin numero.
  80.  */
  81.  
  82.     for (i=1; i<argc; i++) if (stringcmp(args[i], "unit") == TRUE) unit = args[i+1][0] - '0';
  83.     if (unit == 500) cleanup("*** No unit number specified!");
  84.  
  85. /*
  86.  * Sitten startcyl ja endcyl
  87.  */
  88.  
  89.     for (i=1; i<argc; i++) if (stringcmp(args[i], "startcyl") == TRUE)
  90.         if (strlen(args[i+1]) == 2)
  91.             { startcyl = 10*(args[i+1][0]-'0')+args[i+1][1]-'0'; }
  92.         else
  93.             { startcyl = args[i+1][0]-'0'; }
  94.  
  95.     for (i=1; i<argc; i++) if (stringcmp(args[i], "endcyl") == TRUE)
  96.         if (strlen(args[i+1]) == 2)
  97.             { endcyl = 10*(args[i+1][0]-'0')+args[i+1][1]-'0'; }
  98.         else
  99.             { endcyl = args[i+1][0]-'0'; }
  100.  
  101.     if (startcyl > 79) startcyl = 79;
  102.     if (endcyl > 79) endcyl = 79;
  103.     if (startcyl > endcyl) { i = startcyl; startcyl = endcyl; endcyl = i; }
  104.  
  105.  
  106. /*
  107.  * Sitten tulostetaan hieman infoa missä mennään: (debug)
  108.  */
  109.  
  110. //    printf("Debug info:\n comm:\t%ld\n unit:\t%ld\n start\t%ld\n end:\t%ld\n len:\t%ld\n file:\t%s\n", action, unit, startcyl, endcyl, (endcyl-startcyl+1)*cylsize, fname);
  111.  
  112. /*
  113.  * Ja sitten rokataan!!
  114.  */
  115.  
  116.     trackflag = OpenDevice("trackdisk.device", unit, io, 0);
  117.     if (trackflag != 0) cleanup("Unable to open trackdisk.device");
  118.  
  119.     buffer = (UBYTE *)AllocMem(cylsize, MEMF_ANY|MEMF_CLEAR);
  120.     if (buffer == NULL) cleanup("Not enough memory!");
  121.  
  122. //    printf("Working...\n");
  123.  
  124.     if (action == cmd_write)
  125.         {
  126.         file = (APTR)Open(fname, MODE_OLDFILE);
  127.         if (file == NULL)
  128.             {
  129.             FreeMem(buffer, cylsize);
  130.             cleanup("*** Unable to open file");
  131.             }
  132.         for (i=startcyl; i<endcyl+1; i++)
  133.             {
  134.             Read(file, buffer, cylsize);
  135.             io->io_Command = CMD_WRITE;
  136.             io->io_Length = cylsize;
  137.             io->io_Data = buffer;
  138.             io->io_Offset = i*cylsize;
  139.             DoIO(io);
  140.             printf("Writing cylinder %ld. Bytes written: %ld\13\n", i, (i-startcyl+1)*cylsize);
  141.             }
  142.         Close(file);
  143.         }
  144.     else
  145.         {
  146.         file = (APTR)Open(fname, MODE_NEWFILE);
  147.         if (file == NULL)
  148.             {
  149.             FreeMem(buffer, cylsize);
  150.             cleanup("*** Unable to open file!");
  151.             }
  152.         for (i=startcyl; i<endcyl+1; i++)
  153.             {
  154.             io->io_Command = CMD_READ;
  155.             io->io_Length = cylsize;
  156.             io->io_Data = buffer;
  157.             io->io_Offset = i*cylsize;
  158.             DoIO(io);
  159.             Write(file, buffer, cylsize);
  160.             printf("Reading cylinder %ld. Bytes read: %ld\13\n", i, (i-startcyl+1)*cylsize);
  161.             }
  162.         Close(file);
  163.         }
  164.  
  165.     printf("\n");
  166.     FreeMem(buffer, cylsize);
  167.     cleanup(NULL);
  168.     }
  169.  
  170. /*****************************************************************************/
  171. /* siivous rutkut */
  172.  
  173. void cleanup(char *error)
  174.     {
  175.     if (trackflag == NULL) CloseDevice(io);
  176.     if (msg != NULL) DeleteMsgPort(msg);
  177.     if (io != NULL) DeleteIORequest(io);
  178.  
  179.     if (error != NULL)
  180.         {
  181.         printf("%s\n", usagetxt);
  182.         printf("%s\n", error);
  183.         }
  184.  
  185.     exit(0);
  186.     }
  187.  
  188. /*****************************************************************************/
  189. /* Pikku aliohjelma stringien vertailuun */
  190.  
  191. BOOL stringcmp(char *t1, char *t2)
  192.     {
  193.     ULONG i;
  194.  
  195.     for (i=0; i<strlen(t1); i++)
  196.         if ((t1[i] > 'A'-1) && (t1[i] < 'Z'+1)) t1[i] = t1[i]+32;
  197.  
  198.     if (strlen(t1) != strlen(t2)) return(FALSE);
  199.  
  200.     for (i=0; i<strlen(t1); i++)
  201.         if (t1[i] != t2[i]) return(FALSE);
  202.  
  203.     return(TRUE);
  204.     }
  205.